﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GestorDeFlotasDesktop.Login
{
    public partial class LostPassword : MasterPage
    {
        Form ventanaPadre;
        public capaDatos.SQLSyntaxisGeneration instanciaDatos = new capaDatos.SQLSyntaxisGeneration();
        string pass;
        public LostPassword()
        {
            InitializeComponent();
        }
        public LostPassword(Form ventana_Padre)
        {
            InitializeComponent();
            ventanaPadre = ventana_Padre;

            ventanaPadre.Visible = false;
                    }

        private void button1_Click(object sender, EventArgs e)
        {

            DataTable dt;
            ArrayList parametrosSql = new ArrayList();
            parametrosSql = parametrosStoredProcedure(paramsNames("nombreUser"), paramsValues(txtUserName.Text));
            dt = instanciaDatos.executeSp("PELUSA.Buscar_DatosUsuario", parametrosSql);
            if (dt.Columns.Count == 2)
            {
                pass = Convert.ToString(dt.Rows[0].ItemArray[1]);
                txtPregSec.Text = Convert.ToString(dt.Rows[0].ItemArray[0]);
            }
            else
            {
                MessageBox.Show("Nombre de usuario Inexistente", "Aceptar", MessageBoxButtons.OK);
            }
           }

        private void LostPassword_FormClosing(object sender, FormClosingEventArgs e)
        {
            ventanaPadre.Visible = true;
        }

        private void btResetPass_Click(object sender, EventArgs e)
        {
            string passwString;
            DataTable dt;
            ArrayList parametrosSql = new ArrayList();
            
            if (label3.Text == "Nueva contraseña")
            {

                if (txtPregSec.Text == txtRstaSec.Text)
                    {
                        passwString = BitConverter.ToString(encriptar(txtRstaSec.Text));
                        parametrosSql = parametrosStoredProcedure(paramsNames("nombreUser", "passwordUser"), paramsValues(txtUserName.Text, passwString));
                    dt = instanciaDatos.executeSp("PELUSA.Update_Password", parametrosSql);

                        MessageBox.Show("Su contraseña ha sido modificada ", "Aceptar", MessageBoxButtons.OK);
                       
                        ventanaPadre.Show();
                        this.Close();
                    }
                else
                {
                     MessageBox.Show("Las contraseñas otorgadas no son las mismas ", "Aceptar", MessageBoxButtons.OK);

                    }
            }else if (BitConverter.ToString(encriptar(txtRstaSec.Text)) == pass)
            {
                                
                    label3.Text = "Nueva contraseña";
                    label4.Text = "Ingresela nuevamente";
                    txtPregSec.Text = "";
                    txtRstaSec.Text = "";
                    txtPregSec.PasswordChar = '*';
                    
                    // showForm(new GestorDeFlotasDesktop.Login.NuevoPass(this, txtUserName.Text));
                }
                else
                {
                    MessageBox.Show("Respuesta incorrecta \n Porfavor ingresela nuevamente ", "Aceptar", MessageBoxButtons.OK);
                }
            
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void LostPassword_Load(object sender, EventArgs e)
        {

        }

        //private void LostPassword_Load(object sender, EventArgs e)
        //{

        //}

        //private void label3_Click(object sender, EventArgs e)
        //{
        
        //}
    }
}